package com.situ.mall.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.situ.common.util.PageResult;
import com.situ.mall.mapper.ProductMapper;
import com.situ.mall.pojo.entity.Product;
import com.situ.mall.pojo.query.ProductQuery;
import com.situ.mall.pojo.vo.ProductVO;
import com.situ.mall.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProductServiceImpl implements IProductService {
    @Autowired
    private ProductMapper productMapper;

    @Override
    public PageResult listPortal(ProductQuery productQuery) {

        //使用PageHelper开启分页
        PageHelper.startPage(productQuery.getPage(), productQuery.getLimit());
        //开启分页之后 LIMIT offset,pageSize 这个代码PageHelper帮我们做了
        List<ProductVO> list = productMapper.listPortal(productQuery);
        //查找总数量PageHelper会自动帮我们完成
        //int totalCount = userMapper.selectTotalCount(userQuery);
        PageInfo pageInfo = new PageInfo(list);
        int totalCount = (int) pageInfo.getTotal();

        return PageResult.ok(totalCount, list);
    }

    @Override
    public Product selectByProductId(Integer id) {
        return productMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Product> ProductByPatientId(Integer parentId) {
        return productMapper.ProductByPatientId(parentId);
    }

    @Override
    public List<Product> selectList() {
        return productMapper.selectList();
    }
}
